#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
struct node {
    int val;
    int len;
    node(){}
    node(int v,int l):val(v),len(l){}
};
struct qnode{
    int v,index;
    qnode(){}
    qnode(int val,int id): v(val),index(id){}

};


vector<vector<node> > graph;
vector<vector<qnode> > ques;
vector<bool> visited;
vector<int> fa;
vector<int> dis;
vector<int> output;


void add_edge(int u,int v,int l){
<<<<<<< HEAD
    graph[u].push_back(node(v,l));
=======
>>>>>>> a009e814dafc7c6864281885f7e19c4f817f997a
    graph[v].push_back(node(u,l));
}

int Find(int x){
    if (x==fa[x]) return x;
    else return fa[x] = Find(fa[x]);
}

void Union(int x,int y){
    x= Find(x);
    y= Find(y);
    fa[x] = y;
}



void LCA(int u){
<<<<<<< HEAD
    if (visited[u]) return ;
    visited[u] = true;

    for (int i = 0 ; i < graph.size();i++){
        int id = graph[u][i].val;
        if (!visited[id]) {
            dis[id] = dis[u] + graph[u][i].len;
            LCA(id);
            Union(u,id);
            fa[Find(id)] = u;
        }
    }

    for (int i = 0 ;  i < ques.size();i++){
=======
    //cout <<u <<endl;
    //cout << u<<endl;
    for (int i = 0 ; i < graph[u].size();i++){
        int id = graph[u][i].val;
        if (!visited[id]) {
            //cout << id<<endl;
            dis[id] = dis[u] + graph[u][i].len;
            if (!visited[id]){
                visited[id] =true;
                LCA(id);
            }
            fa[Find(id)] = u;

        }
    }

    for (int i = 0 ;  i < ques[u].size();i++){
        //cout << u<<" "<<ques[u][i].v<<endl;
>>>>>>> a009e814dafc7c6864281885f7e19c4f817f997a
        if (visited[ques[u][i].v]) {
            output[ques[u][i].index] = dis[ques[u][i].v] + dis[u] - 2*dis[Find(ques[u][i].v)];
        }
    }
<<<<<<< HEAD
}

int main(){
    freopen("1986.txt","r",stdin);
=======

}

int main(){
   // freopen("1986.txt","r",stdin);
>>>>>>> a009e814dafc7c6864281885f7e19c4f817f997a

    int n , m,u,v,l;
    scanf("%d%d",&n,&m);
    graph.resize(n+1);
    graph.clear();
    for (int i = 0 ; i<  m ; i++){
        scanf("%d%d%d%*s",&u,&v,&l);
        //cout << u << " "<<v<<" "<<l<<endl;
        add_edge(u,v,l);
        add_edge(v,u,l);
    }

    scanf("%d",&m);
    ques.resize(n+1);
    for (int i = 0 ; i < m; i++){
        scanf("%d%d",&u,&v);
        ques[u].push_back(qnode(v,i));
        ques[v].push_back(qnode(u,i));
    }

    visited.resize(n+1,false);
    dis.resize(n+1,0);
    output.resize(n+1,0);
    for (int i = 0 ; i<=n ; i++) fa.push_back(i);

<<<<<<< HEAD
    LCA(1);
=======
    add_edge(0,1,0);
    add_edge(1,0,0);
    LCA(0);
>>>>>>> a009e814dafc7c6864281885f7e19c4f817f997a
    for (int i = 0 ; i < m ; i++) cout <<output[i]<<endl;

    return 0;
}
